package com.docker.jy.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.docker.chy.domain.DoctorPatient;
import com.docker.chy.mapper.DoctorPatientMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.docker.common.annotation.Log;
import com.docker.common.core.controller.BaseController;
import com.docker.common.core.domain.AjaxResult;
import com.docker.common.enums.BusinessType;
import com.docker.jy.domain.PointsManagement;
import com.docker.jy.service.IPointsManagementService;
import com.docker.common.utils.poi.ExcelUtil;
import com.docker.common.core.page.TableDataInfo;

/**
 * 积分管理Controller
 * 
 * @author ruoyi
 * @date 2025-04-12
 */
@RestController
@RequestMapping("/jy/pointsManagement")
public class PointsManagementController extends BaseController
{
    @Autowired
    private IPointsManagementService pointsManagementService;
    @Autowired
    private DoctorPatientMapper doctorPatientMapper;

    /**
     * 查询积分管理列表
     */
    @PreAuthorize("@ss.hasPermi('jy:pointsManagement:list')")
    @GetMapping("/list")
    public TableDataInfo list(PointsManagement pointsManagement)
    {
        startPage();
        List<PointsManagement> list = pointsManagementService.selectPointsManagementList(pointsManagement);
        //通过patient_id一对一查询患者信息
        for (PointsManagement pointsManagement1 : list) {
            DoctorPatient doctorPatient = doctorPatientMapper.selectDoctorPatientById(Long.valueOf(pointsManagement1.getPatientId()));
            pointsManagement1.setDoctorPatient(doctorPatient);
        }
        return getDataTable(list);
    }

    /**
     * 导出积分管理列表
     */
    @PreAuthorize("@ss.hasPermi('jy:pointsManagement:export')")
    @Log(title = "积分管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PointsManagement pointsManagement)
    {
        List<PointsManagement> list = pointsManagementService.selectPointsManagementList(pointsManagement);
        ExcelUtil<PointsManagement> util = new ExcelUtil<PointsManagement>(PointsManagement.class);
        util.exportExcel(response, list, "积分管理数据");
    }

    /**
     * 获取积分管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('jy:pointsManagement:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(pointsManagementService.selectPointsManagementById(id));
    }

    /**
     * 新增积分管理
     */
    @PreAuthorize("@ss.hasPermi('jy:pointsManagement:add')")
    @Log(title = "积分管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PointsManagement pointsManagement)
    {
        return toAjax(pointsManagementService.insertPointsManagement(pointsManagement));
    }

    /**
     * 修改积分管理
     */
    @PreAuthorize("@ss.hasPermi('jy:pointsManagement:edit')")
    @Log(title = "积分管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PointsManagement pointsManagement)
    {
        return toAjax(pointsManagementService.updatePointsManagement(pointsManagement));
    }

    @PreAuthorize("@ss.hasPermi('jy:pointsManagement:edit')")
    @Log(title = "积分管理", businessType = BusinessType.UPDATE)
    @PutMapping("/increasePoints")
    public AjaxResult increasePoints(@RequestBody Map<String, Object> params) {
        Long id = Long.valueOf(params.get("id").toString());
        Long points = Long.valueOf(params.get("points").toString());
        return toAjax(pointsManagementService.updatePoints(id, points, true));
    }

    @PreAuthorize("@ss.hasPermi('jy:pointsManagement:edit')")
    @Log(title = "积分管理", businessType = BusinessType.UPDATE)
    @PutMapping("/decreasePoints")
    public AjaxResult decreasePoints(@RequestBody Map<String, Object> params) {
        Long id = Long.valueOf(params.get("id").toString());
        Long points = Long.valueOf(params.get("points").toString());
        return toAjax(pointsManagementService.updatePoints(id, points, false));
    }

    /**
     * 删除积分管理
     */
    @PreAuthorize("@ss.hasPermi('jy:pointsManagement:remove')")
    @Log(title = "积分管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pointsManagementService.deletePointsManagementByIds(ids));
    }
}
